package cn.kattomy.shop.order.dao.impl;

import java.util.ArrayList;
import java.util.List;

import cn.kattomy.shop.base.dao.BaseDao;
import cn.kattomy.shop.order.dao.OrderDao;
import cn.kattomy.shop.order.vo.Order;
import cn.kattomy.shop.order.vo.OrderItem;

public class OrderDaoImpl extends BaseDao implements OrderDao {

	@Override
	public void save(Order order) {
		getSession().save(order);;
	}

	@Override
	public Integer findByCountUid(Integer uid) {
		String hql = "select count(*) from Order o where o.user.uid = ?";
		List<Long> list = getSession().createQuery(hql).setInteger(0, uid).list();
		if (list != null && list.size() > 0) {
			return list.get(0).intValue();
		}
		return null;
	}

	@Override
	public List<Order> findByPageuid(Integer uid, Integer begin, Integer limit) {
		String hql = "from Order o where o.user.uid = ? order by ordertime desc";
		List<Order> list = getSession().createQuery(hql).setInteger(0, uid).setFirstResult(begin).setMaxResults(limit).list();
		return list;
	}

	@Override
	public Order findByOid(Integer oid) {
		return (Order) getSession().get(Order.class, oid);
	}

	@Override
	public void update(Order currOrder) {
		getSession().saveOrUpdate(currOrder);
	}

	@Override
	public int findByCount() {
		String hql = "select count(*) from Order";
		List<Long> list = getSession().createQuery(hql).list();
		if (list != null && list.size() > 0) {
			return list.get(0).intValue();
		}
		return 0;
	}

	@Override
	public List<Order> findByPage(int begin, Integer pageSize) {
		String hql = "from Order order by ordertime desc";
		List<Order> list = getSession().createQuery(hql).setFirstResult(begin).setMaxResults(pageSize).list();
		if (list != null && list.size() > 0) {
			return list;
		}
		return null;
	}

	@Override
	public List<OrderItem> findOrderItem(Integer oid) {
		String hql = "Select oi from OrderItem oi where oi.order.oid = ? order by itemid";
		System.out.println(111);
		List<OrderItem> list = getSession().createQuery(hql).setInteger(0, oid).list();
		System.out.println(222);
		if (list != null && list.size() > 0) {
			return list;
		}
		return null;
	}

	@Override
	public void delete(Order order) {
		getSession().delete(order);
	}
}
